package com.itdfq.resttemplate.controller;

import com.itdfq.resttemplate.entity.HouseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Author GocChin
 * @Date 2021/7/20 11:00
 * @Blog: itdfq.com
 * @QQ: 909256107
 * @Description:
 */
@RestController
public class HouseClientController {
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 发送Get请求，传递参数
     *
     * @param name
     * @return
     */
    @GetMapping("/test1/data")
    public String test1(@RequestParam("name") String name) {
        //使用getForObject
        String forObject = restTemplate.getForObject("http://localhost:8081/house/data?name=" + name, String.class);
        System.out.println(forObject);
        //getForEntity
        ResponseEntity<HouseInfo> forEntity = restTemplate.getForEntity("http://localhost:8081/house/data?name=123", HouseInfo.class);
        System.out.println(forEntity);
        System.out.println(forEntity.getStatusCodeValue());
        System.out.println("HouseInfo:" + forEntity.getBody());
        return forObject;
    }

    /**
     * 动态接受Get请求参数
     *
     * @param name
     * @return
     */
    @GetMapping("/test2/data/{name}")
    public String test2(@PathVariable("name") String name) {
        return restTemplate.getForObject("http://localhost:8081/house/data/{name}", String.class, name);
    }


    /**
     * 发送post请求 不带请求头的方法
     */
    @GetMapping("/test3/save")
    public void test3() {
//        restTemplate.exchange();
        HouseInfo houseInfo2 = new HouseInfo(2l, "杭州", "余杭", "桃花港");
        HouseInfo houseInfo1 = restTemplate.postForObject("http://localhost:8081/house/save", houseInfo2, HouseInfo.class);
        System.out.println(houseInfo1);
    }


    /**
     * 带请求头的 post方法
     * restTemplate.postForEntity():返回的信息比较全面 包含请求body headers 状态等等
     */
    @GetMapping("/test4/save")
    public void test4() {
//        restTemplate.exchange();
        HouseInfo houseInfo2 = new HouseInfo(2l, "杭州", "余杭", "桃花港");
        //设置header信息
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
        requestHeaders.add("token", "123456");
        //封装信息
        HttpEntity requestEntity = new HttpEntity(houseInfo2, requestHeaders);
        HouseInfo houseInfo1 = restTemplate.postForObject("http://localhost:8081/house/save", requestEntity, HouseInfo.class);
        System.out.println(houseInfo1);
    }

    /**
     * exchange 可以请求
     * GET,
     * HEAD,
     * POST,
     * PUT,
     * PATCH,
     * DELETE,
     * OPTIONS,
     * TRACE;
     * 使用exchange 需要制定请求的方式，以及请求参数
     */
    @GetMapping("/test5/save")
    public void test5() {
        HouseInfo houseInfo2 = new HouseInfo(2l, "杭州", "余杭", "桃花港");
        //设置header信息
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.setContentType(MediaType.APPLICATION_JSON);
        requestHeaders.add("token", "123456");
        //封装信息
        HttpEntity requestEntity = new HttpEntity(houseInfo2, requestHeaders);
        ResponseEntity<HouseInfo> exchange = restTemplate.exchange("http://localhost:8081/house/save", HttpMethod.POST, requestEntity, HouseInfo.class);
        System.out.println(exchange.getBody());
        System.out.println("请求的状态："+exchange.getStatusCodeValue());
    }


}
